package org.xlp.validate.code;

import org.xlp.assertion.AssertUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

/**
 * Description: 图片验证码生成器
 * <br/>date: 2024/11/18 18:57
 *
 * @version 1.0
 * @author: xlp
 */
public abstract class AbstractValidateCode implements IValidateCode {
    private int width = 160;
    private int height = 40;
    /**
     * 图片验证码中字符个数
     */
    private int codeCount = 5;
    private int lineCount = 150;
    private String code = null;
    private BufferedImage buffImg = null;

    public AbstractValidateCode() {
        createCode();
    }

    public AbstractValidateCode(int width, int height) {
        this.width = width;
        this.height = height;
        createCode();
    }

    public AbstractValidateCode(int width, int height, int lineCount) {
        this.width = width;
        this.height = height;
        this.lineCount = lineCount;
        createCode();
    }

    public AbstractValidateCode(int width, int height, int codeCount, int lineCount) {
        this.width = width;
        this.height = height;
        this.codeCount = codeCount;
        this.lineCount = lineCount;
        createCode();
    }

    protected void createCode() {
        this.code = randomCode();

        int x = 0;
        int fontHeight = 0;
        int codeY = 0;
        int red = 0;
        int green = 0;
        int blue = 0;

        x = this.width / (this.codeCount + 2);
        fontHeight = this.height - 2;
        codeY = this.height - 4;

        this.buffImg = new BufferedImage(this.width, this.height, 1);
        Graphics2D g = this.buffImg.createGraphics();

        Random random = new Random();

        g.setColor(Color.WHITE);
        g.fillRect(0, 0, this.width, this.height);

        ImgFontByte imgFont = new ImgFontByte();
        Font font = imgFont.getFont(fontHeight);
        g.setFont(font);
        for (int i = 0; i < this.lineCount; i++) {
            int xs = random.nextInt(this.width);
            int ys = random.nextInt(this.height);
            int xe = xs + random.nextInt(this.width / 8);
            int ye = ys + random.nextInt(this.height / 8);
            red = random.nextInt(255);
            green = random.nextInt(255);
            blue = random.nextInt(255);
            g.setColor(new Color(red, green, blue));
            g.drawLine(xs, ys, xe, ye);
        }

        for (int i = 0; i < this.codeCount; i++) {
            red = random.nextInt(255);
            green = random.nextInt(255);
            blue = random.nextInt(255);
            g.setColor(new Color(red, green, blue));
            g.drawString(String.valueOf(this.code.charAt(i)), (i + 1) * x, codeY);
        }
    }

    /**
     * 随机生成字符串验证码，生成的长度需与{@link AbstractValidateCode#codeCount}相同
     * @return 返回随机生成字符串验证码
     */
    protected abstract String randomCode();

    /**
     * 把图片写入指定文件
     * @param path 文件路径
     * @throws IOException
     * @throws NullPointerException 加入参数为空, 则抛出该异常
     */
    public void write(String path) throws IOException {
        AssertUtils.isNotNull(path, "path parameter must not be null!");
        OutputStream sos = new FileOutputStream(path);
        write(sos, true);
    }

    /**
     * 把图片验证码写入输出流
     * @param outputStream
     * @param closeStream true: 关闭流，false：不关闭流
     * @throws IOException
     */
    public void write(OutputStream outputStream, boolean closeStream) throws IOException {
        AssertUtils.isNotNull(outputStream, "outputStream parameter must not be null!");
        try {
            ImageIO.write(this.buffImg, "png", outputStream);
        } finally {
            if (closeStream) {
                outputStream.close();
            }
        }
    }

    /**
     * 把图片验证码写入输出流
     * @param outputStream
     * @throws IOException
     */
    @Override
    public void write(OutputStream outputStream) throws IOException {
        write(outputStream, false);
    }

    @Override
    public BufferedImage getBuffImg() {
        return this.buffImg;
    }

    /**
     * 获取验证码
     * @return
     */
    @Override
    public String getCode() {
        return this.code;
    }

    protected int getWidth() {
        return width;
    }

    protected int getHeight() {
        return height;
    }

    protected int getCodeCount() {
        return codeCount;
    }

    protected int getLineCount() {
        return lineCount;
    }

    protected void setWidth(int width) {
        this.width = width;
    }

    protected void setHeight(int height) {
        this.height = height;
    }

    protected void setCodeCount(int codeCount) {
        this.codeCount = codeCount;
    }

    protected void setLineCount(int lineCount) {
        this.lineCount = lineCount;
    }
}
